<?php

namespace app\admin\controller\basic;

use app\common\controller\Backend;
use think\Db;

/**
 * 版本列表
 */
class Version extends Backend
{

    /**
     * @var \app\common\model\Version
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Version;
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            // 获取请求参数
            $params = $this->request->param();

            // 查询数据（带分页）
            $list = $this->model
                ->order($params['sort'] ?? 'id', $params['order'] ?? 'desc')
                ->paginate([
                    'list_rows' => $params['limit'] ?? 10,
                    'page'      => ($params['offset'] / $params['limit'] + 1) ?? 1
                ]);
            // 返回标准JSON格式
            $result = [
                "total" => $list->total(),
                "rows"  => $list->items()
            ];
            return json($result);
        }

        // 非AJAX请求返回视图
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post('row/a');
            if($data['newversion'] == '' || $data['content'] == '' || $data['downloadurl'] == ''){
                $this->error('参数错误'); 
            }
            $row = $this->model->save($data);
            if ($row) {
                $this->success('添加成功',$row); 
            }else{
                $this->error('添加失败'); 
            }
        }
        return $this->view->fetch();
    }

}